<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>
          <el-icon><Search /></el-icon>
          筛选搜索</span
        >
        <div>
          <el-button class="button" @click="resetFn">重置</el-button>
          <el-button class="button" type="primary" @click="searchParamsFn"
            >查询搜索</el-button
          >
        </div>
      </div>
    </template>
    <div class="search">
      <div class="input">
        <span>输入搜索:</span>
        <el-input v-model="form.orderSn" placeholder="订单编号" />
      </div>
      <div class="input">
        <span>收货人:</span>
        <el-input
          v-model="form.receiverName"
          placeholder="收货人姓名/手机号码"
        />
      </div>
      <div class="input">
        <span>提交时间:</span>
        <div class="block">
          <el-date-picker
            v-model="form.createTime"
            type="date"
            placeholder="请选择时间"
            value-format="YYYY-MM-DD"
          />
        </div>
      </div>
      <div class="input">
        <span>订单状态:</span>
        <div class="select">
          <el-select
            clearable
            v-model="form.status"
            class="m-2"
            placeholder="全部"
            size="large"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
      </div>
      <div class="input">
        <span>订单分类:</span>
        <div class="select">
          <el-select
            clearable
            v-model="form.orderType"
            class="m-2"
            placeholder="全部"
            size="large"
          >
            <el-option
              v-for="item in options3"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
      </div>
      <div class="input">
        <span>订单来源:</span>
        <div class="select">
          <el-select
            clearable
            v-model="form.sourceType"
            class="m-2"
            placeholder="全部"
            size="large"
          >
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
      </div>
    </div>
  </el-card>
  <div class="dataList">
    <div class="dataList-one">
      <el-icon size="large"><Document /></el-icon>
      <span>数据列表</span>
    </div>
    <el-button type="primary" v-permission="'addUser'">添加</el-button>
  </div>
  <el-table
    :data="list"
    style="width: 100%"
    ref="multipleSelection"
    @selection-change="handleSelectionChange"
  >
    <el-table-column type="selection" width="55" />
    <el-table-column fixed prop="id" label="编号" width="150" />
    <el-table-column prop="orderSn" label="订单编号" width="180" />
    <el-table-column prop="createTime" label="提交时间" width="180">
      <template #default="{ row }">
        <span>{{ dateFormat(row.createTime) }}</span></template
      >
    </el-table-column>
    <el-table-column prop="memberUsername" label="用户账号" width="120" />
    <el-table-column prop="totalAmount" label="订单金额" width="120">
      <template #default="{ row }">
        <span>{{ moneyFormat(row.totalAmount) }}</span></template
      >
    </el-table-column>
    <el-table-column prop="payType" label="支付方式" width="120">
      <template #default="{ row }">
        <span>{{ payTypeFn(row.payType) }}</span></template
      >
    </el-table-column>
    <el-table-column prop="sourceType" label="订单来源" width="120">
      <template #default="{ row }">
        <span>{{ row.sourceType === 1 ? "APP订单" : "PC订单" }}</span></template
      >
    </el-table-column>
    <el-table-column prop="status" label="订单状态" width="120">
      <template #default="{ row }">
        <span>{{ orderStatusFn(row.status) }}</span></template
      >
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="180">
      <template #default="{ row }">
        <el-button size="small" @click="handleClick(row.id)"
          >查看订单</el-button
        >
        <el-button
          size="small"
          v-if="row.status !== 4"
          @click="orderTailFn(row.id)"
          >订单跟踪</el-button
        >
        <el-button size="small" type="danger" @click="deleteRow(row.id)" v-else
          >删除订单</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <div class="batch">
    <el-select
      v-model="operation"
      class="m-2"
      placeholder="批量操作"
      size="large"
    >
      <el-option
        v-for="item in options4"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
    </el-select>
    <el-button type="primary" @click="delFn">确定</el-button>
  </div>
  <div class="note">
    <el-dialog
      v-model="isShowDialog"
      title="关闭订单"
      width="30%"
      @close="handleClose"
    >
      <el-input
        v-model="noteValue"
        :rows="2"
        type="textarea"
        placeholder="操作备注信息"
      />
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="EncloseFn"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
  <TraceOrder :id="tailId" />
  <ElConfigProvider :locale="zhCn">
    <div class="right">
      <el-pagination
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        small
        @size-change="pageSizeChangeHandle"
        @current-change="pageChangeHandle"
      />
    </div>
  </ElConfigProvider>
</template>
<script setup lang="ts">
import TraceOrder from "./components/traceorder.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { dateFormat, moneyFormat } from "@/utils";
import { Search, Document } from "@element-plus/icons-vue";
import { reactive } from "vue";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import useTable from "@/hooks/useTable";
import { isShowOrder } from "@/store/showOrder";
import {
  getOrderListApi,
  deleteOrderListApi,
  closeOrderListApi,
} from "./api/api";
import { ref } from "vue";
import { useRouter } from "vue-router";
// 确认关闭订单
const EncloseFn = () => {
  closeOrderListApi(ids.value, noteValue.value).then((res) => {
    if (res.code === 200) {
      isShowDialog.value = false;
      ElMessage({
        message: "关闭成功",
        type: "success",
      });
      searchParamsFn();
    } else {
      ElMessage({
        message: "有巴哥",
        type: "success",
      });
    }
  });
};
// 备注对话框信息
const isShowDialog = ref(false);
// 关闭对话框函数
const handleClose = () => {
  isShowDialog.value = false;
};
// 备注的响应式数据
const noteValue = ref("");
// 批量关闭函数
const closeFn = () => {
  if (ids.value.length === 0) {
    ElMessage({
      type: "warning",
      message: "请选择需要操作的订单",
    });
    return;
  }
  isShowDialog.value = true;
};

const multipleSelection = ref<OrderList[]>([]);
const tailId = ref(0);
// 展示dia对话框
const showTrace = ref(false);
// 关闭dia对话框
const closeDialog = () => {
  showTrace.value = false;
  // if (isReload) queryUserList();
};
const showOrder = isShowOrder();
// 订单跟踪函数
const orderTailFn = (id: number) => {
  showOrder.$state.show = true;
  tailId.value = id;
};
const operation = ref(""); // 批量操作下拉选项
// 批量删除函数
const removeFn = () => {
  if (ids.value.length === 0) {
    ElMessage({
      type: "warning",
      message: "请选择需要操作的订单",
    });
    return;
  }
  ElMessageBox.confirm("您确定要批量删除掉这部分数据吗", "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteOrderListApi(ids.value).then((res) => {
        if (res.code === 200) {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
          removeDataFn();
        }
      });
    })
    .catch(() => {});
};
const delFn = () => {
  // 判断已选的操作方式 0 是批量操作订单 1 是批量关闭订单 2 是批量删除数据
  if (operation.value == "0") {
    // 0 批量发货 1关闭订单 2批量删除
    console.log("批量发货");
  } else if (operation.value == "1") {
    console.log("批量关闭订单");
    closeFn();
  } else if (operation.value == "2") {
    removeFn();
  }
};
const ids = ref<number[]>([]);
//获取多选的数据
const handleSelectionChange = (SelectVal: OrderList[]) => {
  ids.value = [];
  SelectVal.forEach((item: any) => {
    ids.value.push(item.id);
  });
};

// 定点准确单个删除
const deleteRow = (id: number) => {
  console.log("进来了删除", id);
  let ids: number[] = [];
  ids.push(id);
  ElMessageBox.confirm("您确定要批量删除掉这部分数据吗", "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteOrderListApi(ids).then((res) => {
        if (res.code === 200) {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
          removeDataFn();
        }
      });
    })
    .catch(() => {});
};
// 表格事件
const router = useRouter();
// 跳转查看订单页面函数
const handleClick = (id: number) => {
  router.push(`/oms/orderDetail?id=${id}`);
};
// 支付类型处理函数
const payTypeFn = (payType: number) => {
  let payment;
  payment = payType === 0 ? "未支付" : payType === 1 ? "支付宝" : "微信";
  return payment;
};
// 状态处理函数
const orderStatusFn = (status: number) => {
  let orderStatus;
  orderStatus =
    status === 0
      ? "待付款"
      : status === 1
      ? "待发货"
      : status === 2
      ? "已发货"
      : status === 3
      ? "已完成"
      : "已关闭";
  return orderStatus;
};

// 批量操作的数据
const options4 = [
  {
    value: "0",
    label: "批量发货",
  },
  {
    value: "1",
    label: "关闭订单",
  },
  {
    value: "2",
    label: "删除订单",
  },
];
// 订单分类的数据
const options3 = [
  {
    value: "0",
    label: "正常订单",
  },
  {
    value: "1",
    label: "秒杀订单",
  },
];
// 订单来源的数据
const options2 = [
  {
    value: "0",
    label: "PC订单",
  },
  {
    value: "1",
    label: "APP订单",
  },
];
// 订单状态的数据
const options = [
  {
    value: "0",
    label: "待付款",
  },
  {
    value: "1",
    label: "待发货",
  },
  {
    value: "2",
    label: "已发货",
  },
  {
    value: "3",
    label: "已完成",
  },
  {
    value: "4",
    label: "已关闭",
  },
];

// 重置搜索数据函数
const resetFn = () => {
  (form.orderSn = ""),
    (form.receiverName = ""),
    (form.createTime = ""),
    (form.sourceType = ""),
    (form.orderType = ""),
    (form.status = "");
};
// 响应式搜索数据
const form = reactive({
  orderSn: "",
  receiverName: "",
  createTime: "",
  status: "",
  orderType: "",
  sourceType: "",
});
// 封装的useTable导出
const {
  list,
  total,
  pageNum,
  pageSize,
  pageChangeHandle,
  pageSizeChangeHandle,
  searchParamsFn,
  removeDataFn,
} = useTable(getOrderListApi, form);
</script>
<style lang="less" scoped>
.right {
  display: flex;
  justify-content: flex-end;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
:deep(.el-card__header) {
  border: none;
  height: 30px;
}
.search {
  align-items: center;
  width: 700px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin-left: 40px;
  .input {
    display: flex;
    justify-content: space-between;
    width: 350px;
    margin-bottom: 20px;
    align-items: center;
    .select {
      :deep(.el-input__inner) {
        height: 30px;
        width: 185px;
      }
    }
    .block {
      :deep(.el-input__wrapper) {
        height: 30px;
        width: 215px;
        margin-left: -18px;
      }
    }
  }
  //   height: 50px;
  span {
    width: 120px;
    margin-left: 30px;
    font-size: 15px;
  }
}
:deep(.cell) {
  text-align: center;
}
.dataList {
  box-shadow: 0 0 2px 1px #ccc;
  margin-bottom: 20px;
  padding: 10px;
  box-sizing: border-box;
  margin: 10px 0;
  display: flex;
  justify-content: space-between;
  height: 61px;
  align-items: center;
  .dataList-one {
    display: flex;
    align-items: center;
    span {
      margin-left: 10px;
    }
  }
}
.batch {
  margin-top: 20px;
  :deep(.el-input__wrapper) {
    height: 30px;
    width: 160px;
    margin-right: 20px;
  }
}
</style>
